package net.wanho.day02;

import net.wanho.dao.TeacherDaoI;
import net.wanho.entity.Teacher;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class sample {
    static String config="mybatisConfig.xml";
    static SqlSession ss=null;
    static{
        InputStream in= null;
        try {
            in = Resources.getResourceAsStream(config);
        } catch (IOException e) {
            e.printStackTrace();
        }
        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in);
        ss=ssf.openSession();
    }
    public static void main(String[] args) throws IOException, SQLException {
//        method01();
//        method02();
//        method03();
//        updateTname();
//        delete();
//        insert();
//        insert1();
//        selectFemale();
//      findByDepart("发");
        selectTeacher();
    }

    public static void method01() throws IOException {
        InputStream in= Resources.getResourceAsStream(config);
        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in);
        SqlSession ss=ssf.openSession();
        //按照性别获取老师的信息(Tsex)
        List<Teacher> list=ss.selectList("net.wanho.dao.TeacherDao.selectBySex","男");
        System.out.println(list.toString());
//        list=ss.selectList("teacherDao.selectByTno","701");
//        System.out.println(list.toString());
        Teacher teacher=ss.selectOne("net.wanho.dao.TeacherDao.selectByTno","701");
        System.out.println(teacher);
    }

    public static void method02 () throws IOException {
//        InputStream in= Resources.getResourceAsStream(config);
//        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in);
//        SqlSession ss=ssf.openSession();
        TeacherDaoI dao=ss.getMapper(TeacherDaoI.class);
//        List<Teacher> list=dao.selectAll();
//        System.out.println(list.toString());
        Teacher teacher=dao.selectByTno("804");
        System.out.println(teacher);
    }
    public static void method03(){
        TeacherDaoI dao=ss.getMapper(TeacherDaoI.class);
        List<Teacher> list=dao.selectByDepart("开发");
        System.out.println(list.toString());
    }
    public static void updateTname() throws SQLException {
        ss.getConnection().setAutoCommit(true);
        TeacherDaoI dao=ss.getMapper(TeacherDaoI.class);
        dao.updateTname("颓废112","701");
    }

    public static void delete() throws SQLException {
        ss.getConnection().setAutoCommit(true);
        TeacherDaoI dao=ss.getMapper(TeacherDaoI.class);
        dao.deleteByTno("701");
    }

    public static void insert() throws SQLException {
        ss.getConnection().setAutoCommit(true);
        TeacherDaoI dao=ss.getMapper(TeacherDaoI.class);
        dao.insertTeacher("111","name1","男",new Date(),"教授","管理");
    }

    public static void insert1() throws SQLException {
        ss.getConnection().setAutoCommit(true);
        TeacherDaoI dao=ss.getMapper(TeacherDaoI.class);
        dao.insert(new Teacher("704","newguan","男","2021-01-01","讲师","开发部"));
    }

    public static void selectFemale(){
        TeacherDaoI dao=ss.getMapper(TeacherDaoI.class);
        List<Teacher> list=dao.selectFemale();
        System.out.println(list);
    }

    public static void findByDepart(String str){
        TeacherDaoI dao=ss.getMapper(TeacherDaoI.class);
        List<Teacher> list=dao.findByDepart(str);
        System.out.println(list);
    }

    public static void selectTeacher(){
        TeacherDaoI dao=ss.getMapper(TeacherDaoI.class);
        List<Teacher> list=dao.selectTeacher();
        System.out.println(list);
    }
}
